<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
define('GM_PATH', dirname(__FILE__) );
define('ROOT_PATH', dirname(GM_PATH) );
define('FRAMEWORK_PATH', ROOT_PATH . '/framework' );
define('PROTECTED_PATH', ROOT_PATH . '/protected' );
define('APP_PATH', PROTECTED_PATH . '/app/gm' );
define('MODEL_PATH', PROTECTED_PATH . '/model' );
define('TEMPLATE_PATH', PROTECTED_PATH . '/template/gm' );
define('ARMODEL_PATH', PROTECTED_PATH . '/armodel' );
define('UPLOAD_PATH', '/uploadfiles' );
require_once(FRAMEWORK_PATH . '/global.php');
require_once(FRAMEWORK_PATH . '/wenyan.php');
require_once(FRAMEWORK_PATH . '/class/log/log.php');
require_once(FRAMEWORK_PATH . '/class/php-activerecord/ActiveRecord.php');
require_once(APP_PATH . '/core.php');
require_once(MODEL_PATH . '/base.model.php');
//require_once(FRAMEWORK_PATH . '/class/push/jpush.php');
require_once(FRAMEWORK_PATH . '/class/smarty-3.1.18/libs/Smarty.class.php');
require_once(FRAMEWORK_PATH . '/class/PHPExcel_1.8.0/Classes/PHPExcel.php');
require_once(PROTECTED_PATH . '/config.php');

//友盟消息推送
require_once(FRAMEWORK_PATH . '/third/umeng_push/notify.php');

if (!get_magic_quotes_gpc()) {
    $_GET   = addslashes_deep($_GET);
    $_POST  = addslashes_deep($_POST);
    $_COOKIE = addslashes_deep($_COOKIE);
    $_SERVER = addslashes_deep($_SERVER);
}

//初始化smarty
$smarty = new Smarty();
$smarty->setTemplateDir(TEMPLATE_PATH);
$smarty->setCompileDir(ROOT_PATH . '/temp/templates_c/gm/');
$smarty->setCacheDir(ROOT_PATH . '/temp/cache/gm/');

//default return json array
$json = array(
	"data" => null,
	"msg_type" => 0,
	"msg" => "成功",
	"error" => 0
);

$app = isset($_GET['app']) ? strtolower(trim($_GET['app'])) : "home";
$act = isset($_GET['act']) ? strtolower(trim($_GET['act'])) : "index";
//security check file and function
if (preg_match("/^[a-zA-Z0-9_.-]+$/", $app) && preg_match("/^[a-zA-Z0-9_.-]+$/", $act)) {
	$file = APP_PATH."/{$app}.php";
	if (file_exists($file)) {
		require_once($file); 
		if (class_exists($app)) {
		    $class  = new $app();
		    if (method_exists($class, $act)) {
		    	$class->$act();
		    } else {
		    	error('missing method');
		    }
		} else {
			error('missing controller');
		}
	} else {
		//echo json_encode(error('missing controller'));
		error('missing file');
	}
} else {
	error('wrong file');
}

//trigger error, exit
function error($msg, $msg_type=-1, $error=1) {
	global $json, $smarty;
	$json['error'] = $error;
	$json['msg_type'] = $msg_type;
	$json['msg'] = $msg;
	echo '<!DOCTYPE html>
<html lang="en">
	<head>
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
		<meta charset="utf-8" />
		<title>管理后台</title>
		<meta name="description" content="top menu &amp; navigation" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
	</head>
	<body>
	</body>
</html>
<script>
alert("'.$msg.'");
history.go(-1);
</script>
';

	exit;
	//die(json_encode($json));
	//return $json;
}

function display($tpl = '', $data=array()) {
	global $json;
	global $app, $act, $smarty;
	global $image_domain;
	if ($tpl) {
		$template_file =$tpl;
	} else {
		$template_file = $app.".".$act.".html";
		if (!file_exists(TEMPLATE_PATH . '/' . $template_file)) {
			die("Template does not exists.");
		}
	}
	echo $template_file;
	$smarty->assign("data", $data);
	$smarty->display($template_file);	
	exit;
}
